package com.huyiyu.dbproject.controller;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPObject;
import com.huyiyu.dbproject.common.api.CommonResult;
import com.huyiyu.dbproject.dto.Student;
import com.huyiyu.dbproject.dto.StudentDetail;
import com.huyiyu.dbproject.dto.StudentTemp;
import com.huyiyu.dbproject.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

@RestController
@RequestMapping("/student")
public class StudentController {

  @Autowired
  private StudentService studentService;

  @GetMapping("/all")
  public CommonResult getStudents(){
    ArrayList<Student> students = studentService.getStudents();
    if(students==null){
      System.out.println("查询所有学生信息失败");
      return CommonResult.failed();
    }
    System.out.println("查询所有学生信息成功");
    return CommonResult.success(students);
  }

  @GetMapping("/getstudentbyintellectual")
  public CommonResult getStudentsByIntellectual(){
    ArrayList<StudentTemp> students = studentService.getStudentsByIntellectual();
    if(students==null){
//      System.out.println("查询所有学生信息失败");
      return CommonResult.failed();
    }
//    System.out.println("查询所有学生信息成功");
    return CommonResult.success(students);
  }

  @GetMapping("/getstudentbycapacity")
  public CommonResult getStudentsByCapacity(){
    ArrayList<StudentTemp> students = studentService.getStudentsByCapacity();
    if(students==null){
//      System.out.println("查询所有学生信息失败");
      return CommonResult.failed();
    }
//    System.out.println("查询所有学生信息成功");
    return CommonResult.success(students);
  }

  @GetMapping("/getstudentbysno/{Sno}")
  public CommonResult getStudentBySno(@PathVariable Integer Sno){
    ArrayList<Student> student = studentService.getStudentBySno(Sno);
    if(student==null){
      return CommonResult.failed();
    }
    return CommonResult.success(student);
  }

  @GetMapping("/delete/{Sno}")
  public CommonResult deleteStudent(@PathVariable Integer Sno){
    System.out.println(Sno);
    Integer res = studentService.deleteStudent(Sno);
    if(res==null){
      System.out.println("删除失败");
      return CommonResult.failed();
    }
    System.out.println("删除成功");
    return CommonResult.success();
  }

  @PostMapping("/insert")
  public CommonResult insertStudent(@RequestBody JSONObject data){
    System.out.println(data);
    String dataSno = data.getString("Sno");
    int Sno = Integer.parseInt(dataSno);
    ArrayList students=studentService.getStudentBySno(Sno);
    if(students.size()!=0){
      return CommonResult.failed("插入失败， 学生已存在！");
    }
    String Sname=data.getString("Sname");
    String Sclass = data.getString("Sclass");
    String dataISgrade = data.getString("ISgrade");
    String dataIBonus = data.getString("IBonus");
    String dataCSgrade = data.getString("CSgrade");
    String dataCBonus = data.getString("CBonus");
    System.out.println(Sname+"  "+Sclass+" "+dataISgrade+"  "+dataIBonus+" "+dataCSgrade+" "+dataCBonus);
    float ISgrade = Float.parseFloat(dataISgrade);
    float IBonus = Float.parseFloat(dataIBonus);
    float CSgrade = Float.parseFloat(dataCSgrade);
    float CBonus = Float.parseFloat(dataCBonus);
    StudentDetail studentDetail = new StudentDetail(Sno,Sname,Sclass,ISgrade,IBonus,CSgrade,CBonus);
    System.out.println(studentDetail);
    int res = studentService.insertStudent(studentDetail);
    if(res>0){
      return CommonResult.success();
    }
    return CommonResult.failed();
  }

  @PostMapping("/update")
  public CommonResult updateStudent(@RequestBody JSONObject data){
    System.out.println(data);
    String dataSno = data.getString("Sno");
    int Sno = Integer.parseInt(dataSno);
    ArrayList students=studentService.getStudentBySno(Sno);
    if(students.size()==0){
      return CommonResult.failed("更新失败，学生不存在！");
    }
    String Sname=data.getString("Sname");
    String Sclass = data.getString("Sclass");
    String dataISgrade = data.getString("ISgrade");
    String dataIBonus = data.getString("IBonus");
    String dataCSgrade = data.getString("CSgrade");
    String dataCBonus = data.getString("CBonus");
    System.out.println(Sname+"  "+Sclass+" "+dataISgrade+"  "+dataIBonus+" "+dataCSgrade+" "+dataCBonus);
    float ISgrade = Float.parseFloat(dataISgrade);
    float IBonus = Float.parseFloat(dataIBonus);
    float CSgrade = Float.parseFloat(dataCSgrade);
    float CBonus = Float.parseFloat(dataCBonus);
    StudentDetail studentDetail = new StudentDetail(Sno,Sname,Sclass,ISgrade,IBonus,CSgrade,CBonus);
    System.out.println(studentDetail);
    int res = studentService.updateStudent(studentDetail);
    if(res>0){
      return CommonResult.success();
    }
    return CommonResult.failed();
  }
}
